package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.mapper.ShortMessageMapper;
import com.doubi.model.sys.TShortMessage;
import com.doubi.vo.AppUiModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ShortMessageService extends ServiceImpl<ShortMessageMapper, TShortMessage> {

    @Autowired
    private ShortMessageMapper shortMessageMapper;

    /**
     * MrDoubi、
     * 2020年3月10日 11:08:05
     * 获取5分钟内的验证码
     * @param account 账户
     * @return bean
     */
    public TShortMessage getBeanByMinute(String account){
        return shortMessageMapper.getBeanByMinute(account);
    }

    public void updateBeanStatus(String account,String code){
        QueryWrapper<TShortMessage> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",account);
        wrapper.eq("code",code);
        TShortMessage shortMessage = shortMessageMapper.selectOne(wrapper);
        shortMessage.setStatus(1);
        shortMessageMapper.updateById(shortMessage);
    }

    /**
     * MrDoubi
     * 2020年3月10日 10:58:48
     * 发送验证吗
     * @param account 账号
     * @param code 验证码
     * @param userIp 用户ip
     * @return model
     */
    public AppUiModel<String> addVerificationCode(String account, String code, String userIp){
        TShortMessage shortMessage = new TShortMessage();
        shortMessage.setPhone(account);
        shortMessage.setStatus(0);
        shortMessage.setUserIp(userIp);
        shortMessage.setCode(code);
        shortMessage.setCreateTime(new Date());
        shortMessageMapper.insert(shortMessage);
        return new AppUiModel<>(200,"验证码发送成功");
    }
}
